﻿namespace SuperMarieApi.App
{
    using System.Configuration;
    using System.Web.Http;
    using System.Web.Http.Cors;

    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API 配置和服务


            #region cors配置启用

            var allowedMethods = ConfigurationManager.AppSettings["cors:allowedMethods"];
            var allowedOrigin = ConfigurationManager.AppSettings["cors:allowedOrigin"];
            var allowedHeaders = ConfigurationManager.AppSettings["cors:allowedHeaders"];

            var geduCors = new EnableCorsAttribute(allowedOrigin, allowedHeaders, allowedMethods)
                               {
                                   SupportsCredentials = true
                               };

            //跨域配置
            config.EnableCors(geduCors);
            #endregion


            // Web API 路由
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/v1/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}
